#! -*- coding:utf-8 -*-
import logging
from datetime import datetime, timedelta

from django.core.management.base import BaseCommand
from common.order.model import Order, PAY_STATUS, NOTIFY_STATUS
from common.mch import handler as mch_handler

_LOGGER = logging.getLogger('worker')

# cd /home/ubuntu/af-env/hydra;/home/ubuntu/af-env/bin/python manage.py

class Command(BaseCommand):

    def handle(self, the_day=0, **options):
        print 'start'
        _LOGGER.info('repeat_notify_order start')
        start = datetime.now() + timedelta(hours=(-8 - int(the_day) * 24))
        items = Order.query.filter(Order.created_at >= start).filter(
            Order.status == PAY_STATUS.SUCC).filter(Order.notify_status != NOTIFY_STATUS.SUCC).limit(10).all()
        print 'total : %s' % len(items)
        count = 0
        for item in items:
            mch_handler.notify_mch(item.id)
            print count
            count = count + 1
        print 'end'
        _LOGGER.info('repeat_notify_order end')
